package com.wps.wdztv6.controller.extend;

import com.mybatisflex.core.paginate.Page;
import com.wps.wdztv6.api.request.TemplateFileQuery;
import com.wps.wdztv6.common.R;
import com.wps.wdztv6.config.WdztAppInfoConfig;
import com.wps.wdztv6.entity.TemplateFile;
import com.wps.wdztv6.service.TemplateFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/extend/api/templateFile")
@Validated
public class TemplateFileController {
    @Autowired
    private TemplateFileService templateFileService;

    @PostMapping("/save")
    public R<TemplateFile> saveTemplateFile(
            TemplateFile templateFile,
            @RequestHeader(value = WdztAppInfoConfig.USER_TOKEN_KEY) String token
    ) {

        return templateFileService.saveTemplateFile(templateFile, token);
    }

    @PostMapping("/queryByPage")
    public R<Page<TemplateFile>> query(@RequestBody
                                       @Validated
                                       TemplateFileQuery queryPage) {

        Page<TemplateFile> page1 = templateFileService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }


    /**
     * List<Integer> 集合验证 必须在类上也加 @Validated
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public R<TemplateFile> del(@RequestBody
                               @Validated
                               @NotEmpty(message = "ids 集合不能空")
                               List<Integer> ids,
                               @RequestHeader(value = WdztAppInfoConfig.USER_TOKEN_KEY) String token) {

        templateFileService.removeByIdsAndFiles(ids, token);

        return R.success("删除成功");
    }


}
